<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
 class DoctorAppController extends Controller{

     //============================内部函数输出Json=================================
     protected function echoJson($arr){
         echo json_encode($arr);
     }
   //==================================医生视频接诊订单表============================
     public function doctor_orders(){

         if(!$doctor_id=I("request.doctor_id")){
             $this->echoJson(array('result' => 1, 'info' => "参数错误"));exit;
         }

         if(I("request.type")!=""){
             $type=I("request.type");
             $endTime=time();
             if($type==0){//已结束
                 $map['end_time']=array('ELT',$endTime);
               }elseif($type==1){//未开始EGT
                 $map['end_time']=array('EGT',$endTime);
             }
         }
         $order_info=D("Order_info");
         $user=D("User");
         $shopBase=D("Shop_base");
         $goodsBase=D("Goods_base");
         $goods_user=D("Goods_user");


         $map['doctor_id']=$doctor_id;
         $map['lid']=3;
         $map['order_status']=1;
         //传过来type类型如果type==0已结束 type==1 未结束

         $orders=$order_info->where($map)->field('
             id,order_no,uid,addr_id,
             shop_id, goods_ids,goods_total_price,lid,
             rate,optimum_time,end_time,doctor_id')->select();
//         echo $order_info->getLastSql();
         //=============================获取视频订单表的数据===================================

         $arr=array();
         foreach($orders as $key=>$val){
             $arr[$key]['order_no']=$val['order_no'];
             $arr[$key]['shop_id']=$shopBase->where("id=".$val['shop_id'])->getField("name");
             $arr[$key]['rate']=$val['rate'];
             $arr[$key]['optimum_time']=$val['optimum_time'];
             $arr[$key]['end_time']=$val['end_time'];
             $arr[$key]['goods_total_price']=$val['goods_total_price'];

             //==========================获得用户的&性别&姓名==============================
             $userInfo[$key]=$user->where("id=".$val['uid'])->field("id,image,name,sex")->find();
             foreach($userInfo as $k=>$v){
                 $arr[$key]['userid']=$v['id'];
                 $arr[$key]['nickname']=$v['name'];
                 $arr[$key]['image']= (!empty($v['image']))? "http://img.user.ivf-bb.com/".rtrim($v['image'],','):$v['image'];
             }

             //====================获取医生数据=中英介绍&时间&医生姓名=========================
              $map['id']=$val['goods_ids'];
              $goods[$key]= $goodsBase->where($map)->field("name,en_name,abstract,en_abstract,spec,price")->find();
             foreach ($goods as $kk=>$vv) {
                 $arr[$key]['goods_name']=$vv['name'];
                 $arr[$key]['goods_en_name']=$vv['en_name'];
                 $arr[$key]['goods_abstract']=$vv['abstract'];
                 $arr[$key]['goods_en_abstract']=$vv['en_abstract'];
                 $arr[$key]['spec']=$vv['spec'];
                 $arr[$key]['price']=$vv['price'];

              }
           //====================获取用户的真实信息&用户名&==================================
             $map['id']=$val['addr_id'];
             $goodsUser[$key]=$goods_user->where($map)->field("name,en_name,addr,garden,age,description")->find();
             foreach($goodsUser as $kkk=>$vvv){
                 
                 $arr[$key]['username']=$vvv['name'];
                 $arr[$key]['en_username']=$vvv['en_name'];
                 $arr[$key]['addr']=$vvv['addr'];
                 $arr[$key]['en_garden']=$vvv['garden']==1?'Male':'Female';
                 $arr[$key]['garden']=$vvv['garden']==1?'男':'女';
                 $arr[$key]['age']=$vvv['age'];
                 $arr[$key]['description']=$vvv['description'];
             }
         }

         $tempTime=array();//获取时间用于根据时间对数组排序
         foreach($arr as $v1){
             $tempTime[]= date("Y-m-d",$v1['optimum_time']);
         }

         $tempTime=array_unique($tempTime);

         $result=array();
         foreach($tempTime as $k1=>$v2){
             foreach($arr as $v3){

                 if(date("Y-m-d",$v3['optimum_time'])==$v2){
                     //$result[$k1]['times']=$v2;
                     $v3['end_time']=date('H:i',$v3['end_time']);
                     $v3['optimum_time']=date('H:i',$v3['optimum_time']);
                     $result[$v2][]=$v3;
                 }
             }
         }
         $this->echoJson(array('result' => 0, 'info' =>$result));exit;

     }


 }